package net.m2technologies.open_arm.transaction;

import net.m2technologies.open_arm.tranreport.ArmTranReportImpl;
import org.opengroup.arm40.tranreport.ArmTranReport;
import org.opengroup.arm40.transaction.*;

/**
 * Copyright 2005 Mark Masterson<br> <br> Licensed under the Apache License, Version 2.0 (the "License");<br> you may
 * not use this file except in compliance with the License.<br> You may obtain a copy of the License at<br> <br>
 * http://www.apache.org/licenses/LICENSE-2.0<br> <br> Unless required by applicable law or agreed to in writing,
 * software<br> distributed under the License is distributed on an "AS IS" BASIS,<br> WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied.<br> See the License for the specific language governing permissions and<br>
 * limitations under the License.<br>
 * <p/>
 * <p>Description: </p>
 *
 * @author Mark Masterson
 * @version 0.010
 */
public class ArmTransactionDefinitionImpl extends UniqueObjectImpl implements ArmTransactionDefinition {

    private final ArmApplicationDefinition applicationDef;
    private final ArmIdentityPropertiesTransaction transactionIdentityProperties;
    private final String name;

    public ArmTransactionDefinitionImpl(final ArmID id,
                                        final ArmApplicationDefinition applicationDef,
                                        final String uri,
                                        final ArmTransactionFactory creatingFactory) {
        super(id, creatingFactory);
        this.name = uri;
        this.applicationDef = applicationDef;
        final ArmIdentityPropertiesImpl existingProps = (ArmIdentityPropertiesImpl) this.applicationDef
                .getIdentityProperties();
        if (null != existingProps) {
            this.transactionIdentityProperties
                    = new ArmIdentityPropertiesTransactionImpl(new ArmIdentityPropertiesImpl(existingProps));
        } else {
            this.transactionIdentityProperties = null;
        }
    }

    public String getName() {
        return this.name;
    }

    public ArmApplicationDefinition getApplicationDefinition() {
        return this.applicationDef;
    }

    public ArmIdentityPropertiesTransaction getIdentityProperties() {
        return this.transactionIdentityProperties;
    }

    public ArmTransaction getTransaction(final ArmTransactionFactory creatingFactory,
                                         final ArmApplication app,
                                         final String[] contextValues) {
        return new ArmTransactionImpl(creatingFactory, this, app, contextValues);
    }

    public ArmTranReport getTransactionReport(final ArmTransactionFactory creatingFactory) {
        return new ArmTranReportImpl(creatingFactory);
    }

    public boolean equals(final Object o) {
        if (this == o) return true;
        if (!(o instanceof ArmTransactionDefinitionImpl)) return false;
        if (!super.equals(o)) return false;

        final ArmTransactionDefinitionImpl armTransactionDefinition = (ArmTransactionDefinitionImpl) o;

        return null != applicationDef
               ? !applicationDef.equals(armTransactionDefinition.applicationDef)
               : null == armTransactionDefinition.applicationDef && null != name
                 ? !name.equals(armTransactionDefinition.name)
                 : null == armTransactionDefinition.name
                   && !(null != transactionIdentityProperties
                        ? !transactionIdentityProperties.equals(armTransactionDefinition.transactionIdentityProperties)
                        : null != armTransactionDefinition.transactionIdentityProperties);

    }

    public int hashCode() {
        int result = super.hashCode();
        result = HASH_CODE_MAGIC_NUMBER * result + (null != applicationDef ? applicationDef.hashCode() : 0);
        result = HASH_CODE_MAGIC_NUMBER * result
                 + (null != transactionIdentityProperties ? transactionIdentityProperties.hashCode() : 0);
        result = HASH_CODE_MAGIC_NUMBER * result + (null != name ? name.hashCode() : 0);
        return result;
    }
}
